package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import DataObjects.User;

public class GetUserById {
	private User employee;
	private String empId;
	private Vector<Vector> days=new Vector<Vector>();
	
	public GetUserById(String eid)
	{
		empId=eid;
		employee=new User();		
	}
	
	public User get()
	{
		try {
			SQLConnection sc = new SQLConnection();		
			PreparedStatement ps1 = sc.getConnection().prepareStatement("SELECT name,phoneNo,role,hireDate,email,house,city,eid,street,lastName,idNumber,numBranch,numAccount,permission,birthday,nameBank,status FROM Employee WHERE eid=?;");
			ps1.setString(1,empId);
			ResultSet rs1=ps1.executeQuery();
			
			
	        while(rs1.next())
	        {
	        	
	        	employee.setName(rs1.getString(1));
	        	employee.setPhoneNo(rs1.getString(2));
	        	employee.setRole(rs1.getString(3));
	        	employee.setHireDayDate(rs1.getDate(4));
	        	employee.setEmail(rs1.getString(5));
	        	employee.setHouse(rs1.getString(6));
	        	employee.setCity(rs1.getString(7));
	        	employee.setEid(rs1.getInt(8));
	        	employee.setStreet(rs1.getString(9));
	        	employee.setLastName(rs1.getString(10));
	        	employee.setIdNumber(rs1.getString(11));
	        	employee.setNumBranch(rs1.getString(12));
	        	employee.setNumAccount(rs1.getString(13));
	        	employee.setPermission(rs1.getInt(14));
	        	employee.setBirthdayDate(rs1.getDate(15));
	        	employee.setNameBank(rs1.getString(16));
	        	employee.setStatus(rs1.getString(17));
	        	
	        }
	        ps1.close();
        	rs1.close();
	        
	        if (employee.getPermission()!=0)
	        {
	        	PreparedStatement ps2 = sc.getConnection().prepareStatement("SELECT l.username,l.password FROM Login l,Employee_Login el WHERE el.eid=? and el.username=l.username");
	        	ps2.setString(1,empId);
	        	ResultSet rs2=ps2.executeQuery();
	        	while(rs2.next())
	        	{
	        		employee.setUsername(rs2.getString(1));
	        		employee.setPassword(rs2.getString(2));
	        	}
	        	ps2.close();
	        	rs2.close();
	        }
         
        	PreparedStatement psAlef = sc.getConnection().prepareStatement("SELECT max(CAST(hour AS UNSIGNED INTEGER)),min(CAST(hour AS UNSIGNED INTEGER)) FROM EmployeeDaysOfWeek WHERE eid=? and day='א';");
        	psAlef.setString(1,empId);
            ResultSet rsAlef=psAlef.executeQuery();
            if(rsAlef.next())
            {
            	if (rsAlef.getString(1)!=null)
            	{
            	Vector<String> alef=new Vector<String>();
            	alef.add("א");
            	alef.add(rsAlef.getString(2));
            	alef.add(rsAlef.getString(1));
            	days.add(alef);
            	}
            }
            
            
            
            PreparedStatement psBet = sc.getConnection().prepareStatement("SELECT max(CAST(hour AS UNSIGNED INTEGER)),min(CAST(hour AS UNSIGNED INTEGER)) FROM EmployeeDaysOfWeek WHERE eid=? and day='ב';");
        	psBet.setString(1,empId);
            ResultSet rsBet=psBet.executeQuery();
            if(rsBet.next())
            {
            	if (rsBet.getString(1)!=null)
            	{
            	Vector<String> bet=new Vector<String>();
            	bet.add("ב");
            	bet.add(rsBet.getString(2));
            	bet.add(rsBet.getString(1));
            	days.add(bet);
            	}
            }
            
            
            PreparedStatement psGimel = sc.getConnection().prepareStatement("SELECT max(CAST(hour AS UNSIGNED INTEGER)),min(CAST(hour AS UNSIGNED INTEGER)) FROM EmployeeDaysOfWeek WHERE eid=? and day='ג';");
        	psGimel.setString(1,empId);
            ResultSet rsGimel=psGimel.executeQuery();
            if(rsGimel.next())
            {
            	if (rsGimel.getString(1)!=null)
            	{
            	Vector<String> gimel=new Vector<String>();
            	gimel.add("ג");
            	gimel.add(rsGimel.getString(2));
            	gimel.add(rsGimel.getString(1));
            	days.add(gimel);
            	}
            }
            
            
            PreparedStatement psDalet = sc.getConnection().prepareStatement("SELECT max(CAST(hour AS UNSIGNED INTEGER)),min(CAST(hour AS UNSIGNED INTEGER)) FROM EmployeeDaysOfWeek WHERE eid=? and day='ד';");
        	psDalet.setString(1,empId);
            ResultSet rsDalet=psDalet.executeQuery();
            if(rsDalet.next())
            {
            	if (rsDalet.getString(1)!=null)
            	{
            	Vector<String> dalet=new Vector<String>();
            	dalet.add("ד");
            	dalet.add(rsDalet.getString(2));
            	dalet.add(rsDalet.getString(1));
            	days.add(dalet);
            	}
            }
            
            
            PreparedStatement psHey = sc.getConnection().prepareStatement("SELECT max(CAST(hour AS UNSIGNED INTEGER)),min(CAST(hour AS UNSIGNED INTEGER)) FROM EmployeeDaysOfWeek WHERE eid=? and day='ה';");
        	psHey.setString(1,empId);
            ResultSet rsHey=psHey.executeQuery();
            if(rsHey.next())
            {
            	if (rsHey.getString(1)!=null)
            	{
            	Vector<String> hey=new Vector<String>();
            	hey.add("ה");
            	hey.add(rsHey.getString(2));
            	hey.add(rsHey.getString(1));
            	days.add(hey);
            	}
            }
            
            
            PreparedStatement psVav = sc.getConnection().prepareStatement("SELECT max(CAST(hour AS UNSIGNED INTEGER)),min(CAST(hour AS UNSIGNED INTEGER)) FROM EmployeeDaysOfWeek WHERE eid=? and day='ו';");
        	psVav.setString(1,empId);
            ResultSet rsVav=psVav.executeQuery();
            if(rsVav.next())
            {
            	if (rsVav.getString(1)!=null)
            	{
            	Vector<String> vav=new Vector<String>();
            	vav.add("ו");
            	vav.add(rsVav.getString(2));
            	vav.add(rsVav.getString(1));
            	days.add(vav);
            	}
            }
            
            
            PreparedStatement psShabat = sc.getConnection().prepareStatement("SELECT max(CAST(hour AS UNSIGNED INTEGER)),min(CAST(hour AS UNSIGNED INTEGER)) FROM EmployeeDaysOfWeek WHERE eid=? and day='שבת';");
        	psShabat.setString(1,empId);
            ResultSet rsShabat=psShabat.executeQuery();
            if(rsShabat.next())
            {
            	if (rsShabat.getString(1)!=null)
            	{
            	Vector<String> shabat=new Vector<String>();
            	shabat.add("שבת");
            	shabat.add(rsShabat.getString(2));
            	shabat.add(rsShabat.getString(1));
            	days.add(shabat);
            	}
            }
            
            psAlef.close();
            rsAlef.close();
            psBet.close();
            rsBet.close();
            psGimel.close();
            rsGimel.close();
            psDalet.close();
            rsDalet.close();
            psHey.close();
            rsHey.close();
            psVav.close();
            rsVav.close();
            psShabat.close();
            rsShabat.close();
            
	        
			employee.setWeekHours(days);
			sc.getConnection().close();

		} catch (SQLException e) {
			
			e.printStackTrace();
			return null;
			
		}
		
		
		
		return employee;
	}
	

}
